________________________________________________________________________ Chapter 4: Containers 41 ________________________________________________________________________ CHAPTER FOUR: CONTAINERS HyperPAD offers four storage facilities, called containers, in which you can store data. These four containers are fields, variables, the message box, and the selectedText. Each of these containers can hold up to 32000 characters (depending on available memory). The commands get and put allow you to manipulate the contents of containers (Use set with the selectedText). For example: put "hello there" into page field 1; put 18.2 * 56.78 into subTotal; put "Please Wait..." into the message box; set the selectedText to "hello there"; FIELDS Fields are display and retrieval areas for text within your pads. Fields do not have a fixed length, as they do in some database programs. They can hold any length of text up to 32000 characters, making them efficient storage devices. The following examples show how to manipulate a field's contents using the PADtalk commands put and get. put "This is a test" into field "Status"; put field 2 before word 2 of field "Status"; get page field id 6; -- into "it" ________________________________________________________________________ Chapter 4: Containers 42 ________________________________________________________________________ VARIABLES A variable is a named container that you can use to store data within your script. Like fields, variables can hold up to 32000 characters. The name of a variable can be up to 255 characters long, but it is usually beneficial to keep the name a readable length. Variable names must start with a letter, underscore (_) or $ and contain no spaces. The following are examples of variable names: TaxResult R6 i $fixedAmount CalculatedTotal _total_with_adjustments You do not need to declare variables before using them in HyperPAD (as you do in other languages). You just put something into them. Variables in HyperPAD have no type, as they do in other languages. HyperPAD makes no distinction between text and numbers. Thus, the following operations are legal: 10 + i "10" + i (word 2 of "hello 5 there") + 67.89 - i 56 & i & "hello" If you attempt to apply a mathematical operator to text, however, you will receive an error message. For example: 5 + "hello" Because variables in HyperPAD can be either textual or numeric, the number 0 and the text quantity empty ("") have special meaning. In fact, they are equivalent. When a variable is first introduced, its value is initially empty (""). If the variable is then used in a comparison with another text quantity, the value will be empty (no length). If it is in a comparison with a number, its value will be 0. The equivalence of 0 and empty is important when performing comparisons. Consider the following comparison in PADtalk: if i is empty then...... When HyperPAD compares the two quantities (i and empty), it will attempt to convert both to numbers (HyperPAD first tries to compare numbers, then text). If i is initially empty, then the conversion will change the ________________________________________________________________________ Chapter 4: Containers 43 ________________________________________________________________________ value of i to 0. When this occurs, you can rewrite the comparison as follows: if i & "x" is "x" then...... This forces HyperPAD to compare two quantities as text and avoid its internal conversion to numbers. Internally, HyperPAD keeps track of whether a variable is textual or numeric. HyperPAD will not perform any internal conversions if the variable is being used in accordance with its current type. This greatly speeds up script execution by reducing internal conversions. LOCAL VARIABLES A local variable is a temporary storage container used during the execution of a handler. When the handler has completed execution, the local variables are relinquished. The following is a handler that uses a local variable called temp: handler select; begin put 10 * 5 into temp; put temp into page field 1; end; Access to local variables is faster than access to any other type of container. Thus, to speed up a script you may want to copy global variables or field contents into a local variable, like in the following example: put page field 1 into temp; -- make a copy for i = 1 to 10 do if line i of temp is "lawyer" then beep; This example stores the data from a field in the local variable temp to speed up the loop that follows. Every handler reserves a special local variable called it. This is used to hold temporary results and is even used by some commands to return values. For example: get page field 1; -- puts the data into "it" put it into the message box; ask "What is your name?"; -- puts response into "it" if it is "joe" then beep; Using the variable it enhances the readability of your PADtalk scripts. ________________________________________________________________________ Chapter 4: Containers 44 ________________________________________________________________________ GLOBAL VARIABLES Global variables are accessible from all handlers and functions. Global variables stay around until you exit HyperPAD or run another program. You can tell your handler that a variable is global using the global statement. The following statement declares two global variables lineCount and totalPrice: global lineCount,totalPrice; When you are done using global variables, you may want to delete their values to save memory. You can do this using the delete or put command: put empty into lineCount; delete totalPrice; The following handler uses a global variable called previousText to store the user's last response to the ask statement: handler select; begin global previousText; ask "Where do you live" with previousText; put it into previousText; end; PARAMETER VARIABLES Parameter variables are place holders that attach names to values which are passed to handlers and functions. Parameter variables are similar to local variables in that they can only be used within the handler in which they are declared. The following example shows a handler that declares some parameter variables and how to call it: handler CalculateResult(interest,periods); begin put interest * periods / 365 into page field 1; end; handler select; begin CalculateResult 12 / 100,36; end; ________________________________________________________________________ Chapter 4: Containers 45 ________________________________________________________________________ THE MESSAGE BOX The message box is a container that has two uses: 1. You can display messages for the pad user. 2. You can type in commands and execute them immediately. The following example uses the message box to tell the user to wait: show the message box; put "Please Wait..." into the message box; The message box is the default destination of the put command. For example, the following statements do the same thing: put "Please Wait..."; put "Please Wait..." into the message box; The message box can be referred to by: the message box message msg msg box THE SELECTEDTEXT The selectedText is another storage facility into which you can put information. Also, text is automatically stored here when the user highlights text in a field (using the SHIFT+ARROW keys or dragging the mouse). You can use put and get to retrieve its contents: put the selectedText into page field 1; get the selectedText; -- put it into "it" However, unlike other containers, you can only set its value using the set command: set the selectedText to "hello there"; set the selectedText to empty; Changing the value of the selectedText has no effect on the display.